﻿using System;
using System.Net.Mail;
using System.Text;
using SpiderMaster.AspNet.TradingSystem.BusinessLogicLayer;
using SpiderMaster.Net;

namespace SpiderMaster.AspNet.TradingSystem
{
    public sealed partial class MemberManagement
    {
        ///// <summary> Sends the member notification.
        ///// </summary>
        ///// <param name="tcm">The TCM.</param>
        // public static void SendMemberNotification(TradingCenterMember tcm)

        /// <summary> Sends the member notification.
        /// </summary>
        /// <param name="UserName">Name of the user.</param>
        /// <param name="IsApproved">if set to <c>true</c> [is approved].</param>
        /// <param name="IsAspNetImport">if set to <c>true</c> [is ASP net import].</param>
        /// <param name="MemberId">The member id.</param>
        public static void SendMemberNotification(string UserName, bool IsApproved, bool IsAspNetImport, int MemberId)
        {
            StringBuilder messageBody = new StringBuilder();

            messageBody.AppendFormat("A new Member has just registered on the " + Product.Current.Name + " site '{0}':", SiteSettings.Current.SiteName);
            messageBody.AppendLine();

            messageBody.AppendLine();

            messageBody.Append("User Name: ");
            messageBody.AppendLine(UserName);

            messageBody.Append("Status: ");
            messageBody.AppendLine(IsApproved ? "Enabled" : "Disabled");

            messageBody.AppendLine();

            messageBody.AppendLine("For Details and Activation: ");
            messageBody.Append(TradingCenter.Current.SiteUrl);
            messageBody.AppendFormat("/Administration/EditMember.aspx?MemberId={0}", MemberId);

            messageBody.AppendLine();

            if (IsApproved)
                messageBody.AppendLine("Status: The User was activated automatically.".Add(IsAspNetImport ? " User was Imported using Asp Net Member Details." : string.Empty));
            else messageBody.AppendLine("Status: The User is not yet activated.".Add(IsAspNetImport ? " User was Imported using Asp Net Member Details." : string.Empty));

            try
            {
                foreach (string st in RoleManagement.GetRecipients(true, false, PredefinedRole.GeneralAdministrator))
                {
                    using (MailMessage m = new MailMessage(SiteSettings.Current.MailSenderAddressFormatted, st))
                    {
                        m.Subject = String.Format("New User Registered: {0}", UserName);
                        m.Body = messageBody.ToString();
                        EmailManager.SendCompletedEmail(m);
                    }
                }
            }
            catch { }
        }
    }
}